//
//  CZEmoticonToolBar.swift
//  Weibo28
//
//  Created by Apple on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum CZEmoticonToolBarButtonType: Int {
    //  默认
    case Normal = 1000
    //  emoji
    case Emoji = 1001
    //  浪小花
    case Lxh = 1002
}

//  表情键盘toolbar
class CZEmoticonToolBar: UIStackView {

    //  计算上一次选中按钮
    var lastSelectedButton: UIButton?
    
    //  定义点击表情按钮的闭包
    var didSelectedEmoticonButtonClosure: ((type: CZEmoticonToolBarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  布局方式
        axis = .Horizontal
        //  填充方式
        distribution = .FillEqually
        addChildButton("默认", bgImageName: "compose_emotion_table_left", type: .Normal)
        addChildButton("Emoji", bgImageName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImageName: "compose_emotion_table_right", type: .Lxh)
    }
    
    private func addChildButton(title: String, bgImageName: String, type: CZEmoticonToolBarButtonType) {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: .TouchUpInside)
        button.setTitle(title, forState: .Normal)
        //  设置文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        //  字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_selected"), forState: .Selected)
        //  去掉高亮效果
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addArrangedSubview(button)
        //  默认状态选中
        if type == .Normal {
            lastSelectedButton?.selected = false
            button.selected = true
            lastSelectedButton = button
        }
        
    }
    
    //  MARK:   - 点击事件
    @objc private func emoticonButtonClick(button: UIButton) {
        //  如果是同一个按钮则不不执行点击事件
        if button == lastSelectedButton {
            return
        }
        
        //  1.取消上一次选中的状态
        lastSelectedButton?.selected = false
        //  2.当前点击的按钮选中
        button.selected = true
        //  3.记录选中按钮
        lastSelectedButton = button
        //  通过tag转成枚举
        let type = CZEmoticonToolBarButtonType(rawValue: button.tag)!
        //  执行闭包
        didSelectedEmoticonButtonClosure?(type: type)
    }
    
    //  根据传入的section 判断选中哪个按钮
    
    func selectEmotionButtonWithSection(section: Int) {
        //  如果tag是0取到不是按钮,是当前控件
//        let view = viewWithTag(section + 1000)
//        print(view)
        //  根据tag判断是否是同一个按钮,如果是不需要选中
        if section + 1000 == lastSelectedButton?.tag {
            return
        }
        
        
        let button = viewWithTag(section + 1000) as! UIButton
        
        lastSelectedButton?.selected = false
        button.selected = true
        lastSelectedButton = button
        
        print("选中了")
        
        
        
    }
    
    
    
    

}
